import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import {PAGE_HOME, PAGE_LOGIN} from "@/constants/page-url-constants";
import layout from "@/layout/layout.vue";
import comlayout from "@/layout/common-layout.vue";
import i18n from '@/lang/index'

const {t} = i18n.global


export const constantRouterMap = [
  {
    path: PAGE_LOGIN,
    component: () => import("@/pages/login.vue"),
    meta: {
      pageTitle: t('selfAnalysis.Log'),
    },
  },
  {
    path: "/404",
    component: () => import("@/pages/404.vue"),
    hidden: true,
  },

  // {
  //     path: "/home/share",
  //     component: () => import("@/pages/index/homeShare.vue"),
  //     hidden: true,
  // },


];

export const asyncRouterMap: Array<RouteRecordRaw> = [
  {
    path: PAGE_LOGIN,
    component: () => import("@/pages/login.vue"),
    meta: {
      pageTitle: t('selfAnalysis.Log'),
    },
  },
  {
    path: "/404",
    component: () => import("@/pages/404.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/layout",
    name: "layout",
    component: comlayout,
    meta: {title: '全局'},
    children: [
      {
        path: "/home",
        name: 'home',
        component: () => import("@/pages/home.vue"),
        meta: {title: '首页'},
      },
    ],
  },
  {
    path: "/front/index",
    name: "frontIndex",
    meta: {title: '首页'},
    component: () => import("@/pages/front/index.vue"),
  },
  {
    path: "/front/test",
    name: "frontTest",
    meta: {title: '首页'},
    component: () => import("@/pages/front/test.vue"),
  },

  {
    path: "/resource",
    name: "resource",
    component: comlayout,
    redirect: "/resource/label",
    meta: {title: '资源管理'},
    children: [
      {
        path: "label",
        name: "label",
        component: () => import("@/pages/resource/label/index.vue"),
        meta: {title: '标签管理'},
      },
      {
        path: "website",
        name: "website",
        component: () => import("@/pages/resource/website/index.vue"),
        meta: {title: '网站管理'},
      },
      {
        path: "component",
        name: "component",
        component: () => import("@/pages/resource/component/index.vue"),
        meta: {title: '组件管理'},
      },
      {
        path: "tabMenu",
        name: "tabMenu",
        component: () => import("@/pages/resource/tabMenu/index.vue"),
        meta: {title: '标签菜单'},
      },
    ]
  },
  {
    path: "/system",
    name: "system",
    component: comlayout,
    redirect: "/system/user",
    meta: {title: t('selfAnalysis.Systems')},
    children: [
      {
        path: "user",
        name: "user",
        component: () => import("@/pages/system/user/index.vue"),
        meta: {title: t('selfAnalysis.User')},
      },
      {
        path: "role",
        name: "role",
        component: () => import("@/pages/system/role/index.vue"),
        meta: {title: t('selfAnalysis.Role')},
      },
      {
        path: "menu",
        name: "menu",
        component: () => import("@/pages/system/menu/index.vue"),
        meta: {title: t('selfAnalysis.Menu')},
      },
      {
        path: "group",
        name: "group",
        component: () => import("@/pages/system/group/index.vue"),
        meta: {title: t('selfAnalysis.Group')},
      },
      {
        path: "globalParam",
        name: "globalParam",
        component: () => import("@/pages/system/globalParam/index.vue"),
        meta: {title: t('selfAnalysis.Global')},
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  // routes: constantRouterMap,
  routes: asyncRouterMap
});

const defaultPageTitle =
  document.querySelector("head>title")?.textContent || "";
router.afterEach((to, from) => {
  const titleNode = document.querySelector("head>title");
  if (titleNode) {
    titleNode.textContent = (to.meta?.pageTitle || defaultPageTitle) as string;
  }
});

export function resetRouter() {
  const newRouter = router;
  (router as any).matcher = (newRouter as any).matcher; // reset router
}

export default router;
